﻿using System;
using System.Drawing;
using System.Windows.Forms;
using SpeechRecognition.Domain.Input;
using ZedGraph;
using System.Collections.Generic;

namespace SpeechRecognition.UI
{
    public partial class MainForm : Form
    {
        private IWavFileReader reader;
        private IWaveProcessor processor;

        private short[] wavFile;
        private double[] wavFileWithoutLatentPeriod;

        private List<List<double>> spectrs;

        public MainForm(IWavFileReader reader, IWaveProcessor processor)
        {
            this.reader = reader;
            this.processor = processor;
            InitializeComponent();
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.wavFile = this.reader.Read(dialog.FileName);

                PointPairList list = new PointPairList();
                for (int i = 0; i < this.wavFile.Length; i++)
                {
                    list.Add(i + 1, this.wavFile[i]);
                }

                this.uxGraphic.GraphPane.CurveList.Clear();
                LineItem line = this.uxGraphic.GraphPane.AddCurve("Необработанная речь", list, Color.Green);
                line.Symbol.IsVisible = false;
                this.uxGraphic.AxisChange();
                this.uxGraphic.Invalidate();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                wavFileWithoutLatentPeriod = this.processor.CutOffLatentPeriod(wavFile);
                var separated = this.processor.Separate(wavFileWithoutLatentPeriod);
                this.spectrs = this.processor.Matie(separated);
                ////show it on the graphic
                PointPairList list = new PointPairList();
                for (int i = 0; i < this.wavFileWithoutLatentPeriod.Length; i++)
                {
                    list.Add(i + 1, this.wavFileWithoutLatentPeriod[i]);
                }

                this.uxGraphic.GraphPane.CurveList.Clear();
                LineItem line = this.uxGraphic.GraphPane.AddCurve("Речь с отсеченными латентными периодами", list, Color.Green);
                line.Symbol.IsVisible = false;
                this.uxGraphic.AxisChange();
                this.uxGraphic.Invalidate();
            }
            catch (Exception)
            {

                MessageBox.Show("Дельфины почти как бобры, хотя порой их можно спутать с кнопочными бутылками");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                this.uxCb.Items.Clear();
                for (int i = 0; i < this.spectrs.Count; i++)
                {
                    this.uxCb.Items.Add(i);
                }
            }
            catch (Exception)
            {
                
                MessageBox.Show("Дельфины почти как бобры");
            }

        }

        private void uxCb_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sNumber = int.Parse(this.uxCb.Text);
            PointPairList list = new PointPairList();
            for (int i = 0; i < 110; i++)
            {
                list.Add(i, this.spectrs[sNumber][i]);
            }

            GraphPane pane = this.zedGraphControl1.GraphPane;


            this.zedGraphControl1.GraphPane.CurveList.Clear();
            LineItem line = this.zedGraphControl1.GraphPane.AddCurve("Спектр №ч" + sNumber, list, Color.Green);
            line.Symbol.IsVisible = false;
            this.zedGraphControl1.AxisChange();
            this.zedGraphControl1.Invalidate();
        }
    }
}
